﻿using System;
using System.Collections;
using System.Threading;
using System.IO;
using System.Xml;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

using GadgeteerLibrary;

namespace XMLTest
{
    public partial class Program
    {
        private const string USER_ID = "User_id";
        private const string DEVICE_ID = "Id";
        private const string DEVICE_NAME = "Name";
        private const string API_KEY = "APIKey";
        private const string FILE_PATH = "configfile";

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            sdCard.SDCardMounted += sdCard_SDCardMounted;

            if (!sdCard.IsCardMounted)
                sdCard.MountSDCard();
        }

        void sdCard_SDCardMounted(GTM.GHIElectronics.SDCard sender, GT.StorageDevice SDCard)
        {
            Debug.Print("SD Card Mounted");

            AppSettings appSettings = new AppSettings();

            using (FileStream xmlStream = sdCard.GetStorageDevice().OpenRead(FILE_PATH))
            {
                appSettings = ConfigurationManager.ReadConfiguration(xmlStream);
            }

            Debug.Print("User ID: " + appSettings.UserID);
            Debug.Print("Device ID: " + appSettings.DeviceID);
            Debug.Print("API Key: " + appSettings.APIKey);
            Debug.Print("Device Name: " + appSettings.DeviceName);

            //using (XmlReader reader = XmlReader.Create(xmlStream))
            //{
            //    reader.MoveToContent();
            //    while (reader.Read())
            //    {
            //        if (reader.NodeType == XmlNodeType.Element)
            //        {
            //            switch (reader.Name)
            //            {
            //                case USER_ID:
            //                    reader.Read();
            //                    Debug.Print("User ID: " + reader.Value);
            //                    break;
            //                case DEVICE_ID:
            //                    reader.Read();
            //                    Debug.Print("Device ID: " + reader.Value);
            //                    break;
            //                case API_KEY:
            //                    reader.Read();
            //                    Debug.Print("API Key: " + reader.Value);
            //                    break;
            //                case DEVICE_NAME:
            //                    reader.Read();
            //                    Debug.Print("Device Name: " + reader.Value);
            //                    break;
            //                default:
            //                    break;
            //            }
            //        }
            //    }
            //}
        }
    }
}
